/**
 * @param {string} s
 * @return {number}
 */
//  var lengthOfLongestSubstring = function(s) {
//   let temp,left,right,max
//   left = 0
//   right = 1
//   max = 0
//   while(right<s.length){
//     temp = s.slice(left,right)
//     if(temp.indexOf(s.charAt(right))>-1){
//       left++;
//       continue
//     }else {
//       right++
//     }
//     if (right - left > max) { max = right - left }
//   }
//   return max
// };


/**
 * @param {string} s
 * @return {number}
 */
 var lengthOfLongestSubstring = function(s) {
  let left = 0
  let right = 0
  let max = 0
  let window = new Set()
  while(left<=right && right<s.length){
    if(!window.has(s[right])){
      window.add(s[right])
      max = Math.max(right-left+1,max)
      right++
    }else {
      window.delete(s[right])
      left++
    }
  }
  return max
};
console.log(lengthOfLongestSubstring('abcdaaa'));